using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.Toolkit.Common;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Core.Toolkit.Core.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Application
{
    [AutoDi()]
    public class ZY_SafetyEducationService : BaseAppService<ZY_SafetyEducationDto, ZY_SafetyEducation, ZY_SafetyEducationRepository>
    {
        readonly ExcelHelp _excelHelp;
        readonly UserIdentity _userIdentity;
        public ZY_SafetyEducationService(ZY_SafetyEducationRepository repository, ExcelHelp excelHelp, IHttpContextAccessor accessor) : base(repository)
        {
            _excelHelp = excelHelp;
            _userIdentity = accessor.HttpContext.User.GetUserIdentity();
        }



        private LoadPageOffsetModel<ZY_SafetyEducation, ZY_SafetyEducationDto>
            GetLoadPageOffsetMode(PagingModel<ZY_SafetyEducationDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModel(pagingQueryModel);
            var baseQuery = _repository.QueryBase().AsNoTracking();
            var dtoModel = pagingQueryModel.Model;
            var filterList = this.CreateQRList()
            .Add("RecordNumber", "in"); //编号
            if (_userIdentity.Headoffice)
                filterList.Add("CreateFactory", "="); //总部从查询条件取工厂DI
            else
                filterList.Add("CreateFactory", "=", _userIdentity.Factory);
            var filterExpre = filterList.BuildeQR(dtoModel.ToEntity<ZY_SafetyEducation>());
            loadPageOffsetModel.QueryWhere = filterExpre;

            //.Add("RecordNumber", "=") //编号
            //.Add("EducationStartTime", "=") //接受教育开始时间
            //.Add("EducationEndTime", "=") //接受教育结束时间
            //.Add("JobName", "=") //作业名称
            //.Add("Chemical", "=") //危险化学品
            //.Add("JSAResult", "=") //JSA分析结果
            //.Add("Measures", "=") //作业现场具体安全措施
            //.Add("UseMethods", "=") //防护器具使用方法
            //.Add("Knowledge", "=") //事故的预防知识
            //.Add("Experience", "=") //事故案例和经验、教训
            //.Add("OtherContent", "=") //其它培训内容
            //.Add("CreateFactory", "=") //隶属工厂
            //.Add("CreateDept", "=") //添加部门
            //.Add("UpdateDept", "=") //
            //.Add("BusinessId", "=");//


            loadPageOffsetModel.BasqQuery = baseQuery;
            loadPageOffsetModel.Order = orderModel => orderModel.CreateTime;
            //loadPageOffsetModel.Selector = model => new ZY_SafetyEducation()
            //{
            //    Id = model.Id,
            //    RecordNumber = model.RecordNumber,//编号
            //    EducationStartTime = model.EducationStartTime,//接受教育开始时间
            //    EducationEndTime = model.EducationEndTime,//接受教育结束时间
            //    JobName = model.JobName,//作业名称
            //    Chemical = model.Chemical,//危险化学品
            //    JSAResult = model.JSAResult,//JSA分析结果
            //    Measures = model.Measures,//作业现场具体安全措施
            //    UseMethods = model.UseMethods,//防护器具使用方法
            //    Knowledge = model.Knowledge,//事故的预防知识
            //    Experience = model.Experience,//事故案例和经验、教训
            //    OtherContent = model.OtherContent,//其它培训内容
            //    CreateFactory = model.CreateFactory,//隶属工厂
            //    CreateDept = model.CreateDept,//添加部门
            //    UpdateDept = model.UpdateDept,//
            //    BusinessId = model.BusinessId,//
            //};
            return loadPageOffsetModel;
        }


        public async Task<IPageModel<ZY_SafetyEducationDto>> GetList(PagingModel<ZY_SafetyEducationDto>
            pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            return await base.LoadPageOffsetAsync(loadPageOffsetModel);
        }


        public async Task<string>
            Export(PagingModel<ZY_SafetyEducationDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            var DataList = await this.ExportDataAsync(loadPageOffsetModel);
            var excelData = DataList.Select(model => new
            {
                model.RecordNumber,
                RecordNumber_title = "编号",
                model.EducationStartTime,
                EducationStartTime_title = "接受教育开始时间",
                model.EducationEndTime,
                EducationEndTime_title = "接受教育结束时间",
                model.JobName,
                JobName_title = "作业名称",
                model.Chemical,
                Chemical_title = "危险化学品",
                model.JSAResult,
                JSAResult_title = "JSA分析结果",
                model.Measures,
                Measures_title = "作业现场具体安全措施",
                model.UseMethods,
                UseMethods_title = "防护器具使用方法",
                model.Knowledge,
                Knowledge_title = "事故的预防知识",
                model.Experience,
                Experience_title = "事故案例和经验、教训",
                model.OtherContent,
                OtherContent_title = "其它培训内容",
                model.CreateFactory,
                CreateFactory_title = "隶属工厂",
                model.CreateDept,
                CreateDept_title = "添加部门",
                model.UpdateDept,
                UpdateDept_title = "",
                model.BusinessId,
                BusinessId_title = "",
            }).ToList();




            return _excelHelp.Create(excelData);
        }

    }

}
